package carassius.GUI;

import carassius.BLL.Events.Animal.AnimalAddedListener;
import carassius.BLL.Events.Animal.AnimalRemovedListener;
import carassius.DAL.AnimalRow;
import carassius.DAL.AquariumRow;
import carassius.DAL.Database;
import carassius.DAL.OrganismListRow;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.AbstractMap.SimpleEntry;
import java.util.Collection;
import java.util.ResourceBundle;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author siebz0r
 */
public class AddAnimalToAquariumForm
		extends javax.swing.JDialog
		implements AnimalAddedListener,
				   AnimalRemovedListener
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private AquariumRow _aquariumRow;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public AddAnimalToAquariumForm(Window parent, AquariumRow aquariumRow)
	{
		super(parent, ModalityType.APPLICATION_MODAL);
		_aquariumRow = aquariumRow;
		initComponents();

		//temporary disable import function
		uxImportButton.setVisible(false);

		uxAquariumLabel.setText(aquariumRow.getName());
		Database.getAnimalTable().addAnimalAddedEventListener(this);
		Database.getAnimalTable().addAnimalRemovedEventListener(this);
		try
		{
			Collection<AnimalRow> animals = Database.getAnimalTable().getAll();
			DefaultListModel model = new DefaultListModel();
			for (AnimalRow row : animals)
			{
				model.addElement(row);
			}
			uxAnimalList.setModel(model);
		}
		catch (SQLException ex)
		{
			//todo: handle
		}
	}
	//</editor-fold>

	@Override
	public void dispose()
	{
		Database.getAnimalTable().removeAnimalAddedEventListener(this);
		Database.getAnimalTable().removeAnimalRemovedEventListener(this);
		super.dispose();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        uxAquariumPrefixLabel = new javax.swing.JLabel();
        uxAquariumLabel = new javax.swing.JLabel();
        uxAnimalScrollPane = new javax.swing.JScrollPane();
        uxAnimalList = new javax.swing.JList();
        uxButtonPanel = new javax.swing.JPanel();
        uxImportButton = new javax.swing.JButton();
        uxNewButton = new javax.swing.JButton();
        uxAddButton = new javax.swing.JButton();
        uxCancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(new StringBuilder(java.util.ResourceBundle.getBundle("carassius/GUI/resources/AddAnimalToAquariumForm").getString("Form.title")).append(_aquariumRow.getName()).toString());
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(carassius.CarassiusApp.class).getContext().getResourceMap(AddAnimalToAquariumForm.class);
        uxAquariumPrefixLabel.setText(resourceMap.getString("uxAquariumPrefixLabel.text")); // NOI18N
        uxAquariumPrefixLabel.setName("uxAquariumPrefixLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(uxAquariumPrefixLabel, gridBagConstraints);

        uxAquariumLabel.setText(resourceMap.getString("uxAquariumLabel.text")); // NOI18N
        uxAquariumLabel.setName("uxAquariumLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5);
        getContentPane().add(uxAquariumLabel, gridBagConstraints);

        uxAnimalScrollPane.setName("uxAnimalScrollPane"); // NOI18N

        uxAnimalList.setName("uxAnimalList"); // NOI18N
        uxAnimalList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                uxAnimalListMouseClicked(evt);
            }
        });
        uxAnimalList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                uxAnimalListKeyPressed(evt);
            }
        });
        uxAnimalScrollPane.setViewportView(uxAnimalList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        getContentPane().add(uxAnimalScrollPane, gridBagConstraints);

        uxButtonPanel.setName("uxButtonPanel"); // NOI18N

        uxImportButton.setText(resourceMap.getString("uxImportButton.text")); // NOI18N
        uxImportButton.setName("uxImportButton"); // NOI18N
        uxImportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uxImportButtonActionPerformed(evt);
            }
        });
        uxButtonPanel.add(uxImportButton);

        uxNewButton.setText(resourceMap.getString("uxNewButton.text")); // NOI18N
        uxNewButton.setName("uxNewButton"); // NOI18N
        uxNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uxNewButtonActionPerformed(evt);
            }
        });
        uxButtonPanel.add(uxNewButton);

        uxAddButton.setText(resourceMap.getString("uxAddButton.text")); // NOI18N
        uxAddButton.setName("uxAddButton"); // NOI18N
        uxAddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uxAddButtonActionPerformed(evt);
            }
        });
        uxButtonPanel.add(uxAddButton);

        uxCancelButton.setText(resourceMap.getString("uxCancelButton.text")); // NOI18N
        uxCancelButton.setName("uxCancelButton"); // NOI18N
        uxCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uxCancelButtonActionPerformed(evt);
            }
        });
        uxButtonPanel.add(uxCancelButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LAST_LINE_END;
        getContentPane().add(uxButtonPanel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void uxCancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_uxCancelButtonActionPerformed
	{//GEN-HEADEREND:event_uxCancelButtonActionPerformed
		dispose();
	}//GEN-LAST:event_uxCancelButtonActionPerformed

	private void uxAnimalListMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_uxAnimalListMouseClicked
	{//GEN-HEADEREND:event_uxAnimalListMouseClicked
		if (evt.getClickCount() == 2)
		{
			Object selectedValue = uxAnimalList.getSelectedValue();
			if (selectedValue instanceof AnimalRow)
			{
				AnimalForm af = new AnimalForm(this, (AnimalRow) selectedValue);
				af.setVisible(true);
			}
		}
	}//GEN-LAST:event_uxAnimalListMouseClicked

	private void uxAnimalListKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_uxAnimalListKeyPressed
	{//GEN-HEADEREND:event_uxAnimalListKeyPressed
		if (evt.getKeyCode() == KeyEvent.VK_ENTER)
		{
			Object selectedValue = uxAnimalList.getSelectedValue();
			if (selectedValue instanceof AnimalRow)
			{
				AnimalForm af = new AnimalForm(this, (AnimalRow) selectedValue);
				af.setVisible(true);
			}
		}
	}//GEN-LAST:event_uxAnimalListKeyPressed

	private void uxAddButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_uxAddButtonActionPerformed
	{//GEN-HEADEREND:event_uxAddButtonActionPerformed
		Object[] selectedValues = uxAnimalList.getSelectedValues();
		if (selectedValues != null && selectedValues.length > 0)
		{
			boolean result = true;
			try
			{
				for (Object selectedValue : selectedValues)
				{
					if (result && selectedValue instanceof AnimalRow)
					{
						AnimalRow row = (AnimalRow) selectedValue;

						OrganismListRow existingRow = Database.getOrganismListTable().getByPrimaryKey(new SimpleEntry<Integer, Integer>(_aquariumRow.getId(), row.getId()));
						if (existingRow == null)
						{
							if (Database.getOrganismListTable().add(_aquariumRow, row, 1) == null)
							{
								result = false;
								break;
							}
						}
						else
						{
							result = existingRow.setAmount(existingRow.getAmount() + 1);
						}
					}
					else
					{
						result = false;
						break;
					}
				}
			}
			catch (SQLException ex)
			{
				result = false;
			}

			if (result)
			{
				JOptionPane.showMessageDialog(this,
						ResourceBundle.getBundle("carassius.resources.Messages").getString("AddedAnimalToAquarium"),
						ResourceBundle.getBundle("carassius.resources.Messages").getString("AddedAnimalToAquariumTitle"),
						JOptionPane.INFORMATION_MESSAGE);
				dispose();
			}
			else
			{
				JOptionPane.showMessageDialog(this,
						ResourceBundle.getBundle("carassius.resources.Messages").getString("AddAnimalToAquariumError"),
						ResourceBundle.getBundle("carassius.resources.Messages").getString("AddAnimalToAquariumErrorTitle"),
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}//GEN-LAST:event_uxAddButtonActionPerformed

	private void uxNewButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_uxNewButtonActionPerformed
	{//GEN-HEADEREND:event_uxNewButtonActionPerformed
		AnimalForm form = new AnimalForm(this);
		form.setVisible(true);
	}//GEN-LAST:event_uxNewButtonActionPerformed

private void uxImportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uxImportButtonActionPerformed
	OnlineAnimalImporterForm form = new OnlineAnimalImporterForm(this);
	form.setVisible(true);
}//GEN-LAST:event_uxImportButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton uxAddButton;
    private javax.swing.JList uxAnimalList;
    private javax.swing.JScrollPane uxAnimalScrollPane;
    private javax.swing.JLabel uxAquariumLabel;
    private javax.swing.JLabel uxAquariumPrefixLabel;
    private javax.swing.JPanel uxButtonPanel;
    private javax.swing.JButton uxCancelButton;
    private javax.swing.JButton uxImportButton;
    private javax.swing.JButton uxNewButton;
    // End of variables declaration//GEN-END:variables

	//<editor-fold defaultstate="collapsed" desc="Events">
	@Override
	public void animalAdded(AnimalRow row)
	{
		DefaultListModel model = (DefaultListModel) uxAnimalList.getModel();
		model.addElement(row);
	}

	@Override
	public void animalRemoved(AnimalRow row)
	{
		DefaultListModel model = (DefaultListModel) uxAnimalList.getModel();
		model.removeElement(row);
	}
	//</editor-fold>
}
